import hashlib
import hmac

class A:
    def add_sign(self,s,key="hdboms"):
        """
        HMAC+SHA256加密获取签名；并且把签名添加到请求头
        :param key:  如果是b c端app 就是默认值；如果是案场宝app就是hdbscene
        :return:
        """
        strTime = "timestamp="
        strToken = "&token="
        strUnionId = "&unionId="
        print(s)
        print(key)
        try:
            #sign = hmac.new(key.encode(), s.encode(), digestmod=hashlib.sha256).hexdigest().upper()
            sign = hmac.new(key.encode(), s.encode(), digestmod=hashlib.sha256).hexdigest().upper()
        except Exception as e:
            print(False, str(e) + "加密获取sign失败")
            return False
        else:
            # 把签名添加到请求头
            print(True, "请求头中添加sign：" + ">>>" + sign)
        return True


s = "111111"
A().add_sign(s)